package com.mzy.xmyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzy.xmyx.acl.mapper.RoleMapper;
import com.mzy.xmyx.acl.service.AdminRoleService;
import com.mzy.xmyx.acl.service.RoleService;
import com.mzy.xmyx.model.acl.AdminRole;
import com.mzy.xmyx.model.acl.Role;
import com.mzy.xmyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectPage(Integer current, Integer limit, RoleQueryVo roleQueryVo) {
        // 1. 获取查询条件
        String roleName = roleQueryVo.getRoleName();
        // 2. 构建条件构造器
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName, roleName);
        }
        // 3. 构建分页对象
        Page<Role> page = new Page<>(current, limit);

        // 4.执行查询
        IPage<Role> rolePage = roleMapper.selectPage(page, wrapper);
        return rolePage;
    }

    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {

        // 1.获取所有角色id
        List<Role> allRolesList = roleMapper.selectList(null);

        // 2.获取当前已分配角色id
        // 2.1 通过 adminId 查询 admin_role 表，查询用户分配的角色列表
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getAdminId, adminId);

        List<AdminRole> adminRoleList = adminRoleService.list(queryWrapper);

        // 2.2 获取当前用户所有的角色信息
        List<Long> roleIdList = adminRoleList.stream()
                .map(AdminRole::getRoleId)
                .collect(Collectors.toList());

        // 2.3 从所有角色中过滤出已分配的角色信息
        List<Role> assignRoles = allRolesList.stream()
                .filter(role -> roleIdList.contains(role.getId()))
                .collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", allRolesList);
        map.put("assignRoles", assignRoles);
        return map;
    }

    @Override
    public Boolean saveAdminRoleRelationship(Long adminId, List<Long> roleIdList) {
        // 1.删除之前已经分配的角色
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getAdminId, adminId);
        adminRoleService.remove(queryWrapper);
        // 2. 重新分配
        List<AdminRole> adminRoleRelationshipList = roleIdList.stream()
                .map(roleId -> new AdminRole(roleId, adminId))
                .collect(Collectors.toList());
        boolean isSuccess = adminRoleService.saveBatch(adminRoleRelationshipList);

        return isSuccess;
    }


}
